﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Exec.aspx.cs" Inherits="H2.NET.Web.BI.Report.Exec" %>

<style>
    .total {
        text-align:right;
    }
</style>
<script type="text/javascript">
    $(function () {
        $(".total", navTab.getCurrentPanel()).parent().css("background", "#e8edf3").find("td").css("font-weight", "bold");
        if ($(".pageHeader", navTab.getCurrentPanel()).size() > 0) {
            $("[layouth]", navTab.getCurrentPanel()).attr("layouth", ($(".pageHeader", navTab.getCurrentPanel()).outerHeight() + 1 + $("[layouth]", navTab.getCurrentPanel()).attr("layouth")*1));
        }
    });

    function ownPage() {
        var maxPageNum = "<%=totalCount%PageSize==0?totalCount/PageSize: (totalCount/PageSize+1)%>";
         $(".j-num", navTab.getCurrentPanel()).click(function () {
             var val = $(this).find("a").html();
             doSearch(val);
             return false;
         });
         $(".j-prev", navTab.getCurrentPanel()).click(function () {
             if ($(this).hasClass("disabled")) {
                 return false;
             }
             var pre = $(this).parents(".pagination").find("li.selected").find("a").html();
             var val = pre * 1 - 1;
             doSearch(val);
             return false;
         });
         $(".j-next", navTab.getCurrentPanel()).click(function () {
             if ($(this).hasClass("disabled")) {
                 return false;
             }
             var pre = $(this).parents(".pagination").find("li.selected").find("a").html();
             var val = pre * 1 + 1;
             doSearch(val);
             return false;
         });
         $(".j-first", navTab.getCurrentPanel()).click(function () {
             if ($(this).hasClass("disabled")) {
                 return false;
             }
             doSearch(1);
             return false;
         });
         $(".j-last", navTab.getCurrentPanel()).click(function () {
             if ($(this).hasClass("disabled")) {
                 return false;
             }
             doSearch(maxPageNum);
             return false;
         });
         $(".goto", navTab.getCurrentPanel()).click(function () {
             var gotoNum = $(this).prev().val();
             if (gotoNum * 1 > maxPageNum * 1) {
                 gotoNum = maxPageNum;
             }
             doSearch(gotoNum);
             return false;
         });
     }

    function checkNumber(arg) {
        var val = $(arg).val();
        if ($.trim(val)) {
            if (isNaN(val)) {
                $(arg).val("");
                alertMsg.error("请输入数字！");
            }
        }
    }

    function doSearch(page) {
        var where = "";
        var search = "";
        var vals = "";
        var types = "";
        $(".searchContent", navTab.getCurrentPanel()).find("li").each(function () {
            if ($(this).find(":text").size() == 2) {
                var $text1 = $(this).find(":text:first");
                var $text2 = $(this).find(":text:last");
                search +=  $text1.attr("db") + "." + $text1.attr("tb") + "." + $text1.attr("col") + ",";
                if ($text1.val().indexOf(",") >= 0 || $text2.val().indexOf(",") >= 0) {
                    alertMsg.error("查询内容不允许带有逗号 \",\"");
                    return false;
                }
                if ($.trim($text1.val()) || $.trim($text2.val())) {
                    vals += $text1.attr("db") + "." + $text1.attr("tb") + "." + $text1.attr("col") + "=" + $text1.val() + "|" + $text2.val() + ",";
                    var result = "";
                    if ($.trim($text1.val())) {
                        result += $text1.attr("db") + "." + $text1.attr("tb") + "." + $text1.attr("col") + " >= '" + $text1.val() + "'";
                    }
                    if ($.trim($text2.val())) {
                        if (result) {
                            result += " and ";
                        }
                        result += $text2.attr("db") + "." + $text2.attr("tb") + "." + $text2.attr("col") + " <= '" + $text2.val() + "'";
                    }
                    if (!where) {
                        where += " where " + result;
                    } else {
                        where += " and " + result;
                    }
                }
            }
            else {
                var $text = $(this).find(":text");
                if ($(this).find(":text").size() == 0) {
                    var $text = $(this).find(".bit");
                }
                search += $text.attr("db") + "." + $text.attr("tb") + "." + $text.attr("col") + ",";
                var val = $text.val();
                if (val.indexOf(",") >= 0) {
                    alertMsg.error("查询内容不允许带有逗号 \",\"");
                    return false;
                }
                if ($.trim(val)) {
                    var cls = "";
                    var type = " = ";
                    if ($(this).find(":text").size() == 1) {
                        var $select = $(this).find("select").not(".bit");
                        cls = $select.attr("class");
                        type = " " + $select.val() + " ";
                        types += $text.attr("db") + "." + $text.attr("tb") + "." + $text.attr("col") + "=" + $select.val() + ",";
                    }
                    vals += $text.attr("db") + "." + $text.attr("tb") + "." + $text.attr("col") + "=" + val + ",";
                    var result = $text.attr("db") + "." + $text.attr("tb") + "." + $text.attr("col") + type;
                    if (cls == "str") {
                        if (type.indexOf("like") >= 0) {
                            result += "'%" + val + "%'";
                        }
                        else {
                            result += "'" + val + "'";
                        }
                    } else {
                        result += val;
                    }
                    if (!where) {
                        where += " where " + result;
                    } else {
                        where += " and " + result;
                    }
                }
            }
        });
        navTab.openTab("tempQuery", "BI/Report/exec.aspx?action=query&mid=31", {
            title: "报表预览", data:
                { sql: "<%=tempSql%>", totalSql: "<%=totalSql%>", where: where, search: search, widths: '<%:widths%>', ta: '<%:ta%>', types: types, vals: vals, pageNumber: page || 1, pageSize: $(".pageSize", navTab.getCurrentPanel()).val() || 20 }
        });
    }

    
</script>

<%
    if (searchList != null && searchList.Count() > 0)
    {%>
<div class="pageHeader">
	<div class="searchBar">
        <ul  class="searchContent" style="height:auto;">
            <%
                int j = 0;
                foreach (var search in searchList)
                {
                    int val = searchVal.Where(s => s.Key == search.bi_Table.bi_DB.name + ".dbo." + search.bi_Table.table_name + "." + search.column_name).Count();
                    var value = "";
                    if (val>0)
                    {
                        value = searchVal.Where(s => s.Key == search.bi_Table.bi_DB.name + ".dbo." + search.bi_Table.table_name + "." + search.column_name).First().Value;
                    }
                    int tp = searchType.Where(s => s.Key == search.bi_Table.bi_DB.name + ".dbo." + search.bi_Table.table_name + "." + search.column_name).Count();
                    var type = "";
                    if (tp > 0)
                    {
                        type = searchType.Where(s => s.Key == search.bi_Table.bi_DB.name + ".dbo." + search.bi_Table.table_name + "." + search.column_name).First().Value;
                    }
                    %>
                    <li style="height:28px;width:auto;margin-right:50px;">
                        <label style="width:auto;"><%:search.name_cn %>：</label>
                        <%
                    if (search.type == "int" || search.type == "float")
                    {%>
                        <select class="num">
                            <option value="=" <%:type=="="?"selected":"" %>>等于</option>
                            <option value=">" <%:type==">"?"selected":"" %>>大于</option>
                            <option value="<" <%:type=="<"?"selected":"" %>>小于</option>
                        </select>
                    <%}else if(search.type == "string"){%>
                        <select class="str">
                            <option value="=" <%:type=="="?"selected":"" %>>精确</option>
                            <option value="like" <%:type=="like"?"selected":"" %>>模糊</option>
                        </select>
                    <%}
                    if (search.type == "bit")
                    {%>
                        <select class="bit" db="<%:search.bi_Table.bi_DB.name+ ".dbo" %>" tb="<%:search.bi_Table.table_name %>" col="<%:search.column_name %>">
                            <option value="" <%:value==""?"selected":"" %>>全部</option>
                            <option value="1" <%:value=="1"?"selected":"" %>>是</option>
                            <option value="0" <%:value=="0"?"selected":"" %>>否</option>
                        </select>
                   <%}
                     else if(search.type=="datetime")
                     {%>
                        <input type="text" style="float:none" class="date" readonly size="12" value="<%:value!=""?value.Split('|')[0]:"" %>" db="<%:search.bi_Table.bi_DB.name + ".dbo" %>" tb="<%:search.bi_Table.table_name %>" col="<%:search.column_name %>"/>
                        -
                        <input type="text" style="float:none" class="date" readonly size="12" value="<%:value!=""?value.Split('|')[1]:"" %>" db="<%:search.bi_Table.bi_DB.name + ".dbo" %>" tb="<%:search.bi_Table.table_name %>" col="<%:search.column_name %>"/>
                    <%}
                     else{
                      %>   
                        <input type="text" <%:(search.type == "int" || search.type == "float")?"class=number onkeyup=checkNumber(this)":"" %>  value="<%:value %>" db="<%:search.bi_Table.bi_DB.name + ".dbo" %>" tb="<%:search.bi_Table.table_name %>" col="<%:search.column_name %>" />
                    <%} %>
                    </li>
                <% 
                    j++;
                } %>
        </ul>
		<div class="subBar">
            <span class="info_clear">符合条件的记录，共有 <strong><%: totalCount %></strong>条。</span>
			<ul>
				<li><div class="buttonActive"><div class="buttonContent"><button type="button" onclick="doSearch()">检索</button></div></div></li>
                <li><div class="button"><div class="buttonContent"><button type="reset">重置</button></div></div></li>
			</ul>
		</div>
	</div>
</div>
        
    <%}    
 %>
<form id="form1" runat="server">
<div class="pageContent">
    <%--<div class="panelBar">
		<ul class="toolBar">
		    <uc1:toolbar ID="Toolbar1" runat="server" />
		</ul>
	</div>--%>
    <%
        List<String> widthList = widths.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        List<String> taList = ta.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        var sum = widthList.Select(w => int.Parse(w.Substring(w.IndexOf("|") + 1))).Sum();    
     %>
	<table class="table" style="width:<%:sum%>px" layouth="48">
    <thead>
        <tr>
            <th class="fixedcol" style="width:60px">序号</th>
            <%
                if(result!=null){
                int j = 0;
                foreach (var rst in result.Columns)
                {%>
                    <th style="text-align:center;width:<%:widthList[j].Substring(widthList[j].IndexOf("|")+1)%>px;"><%:rst %></th>
                <%
                    j++;
                }    
             %>
        </tr>
    </thead>
    <tbody>
        <%
            int i = 0;
            foreach ( System.Data.DataRow rw in result.Rows)
            {
                i++;
                %>
                <tr>
                     <td class="fixedcol" style="width:60px;"><%:i %></td>
                   <%
                        int k = 0;
                        foreach(var val in rw.ItemArray)
                        {%>
                            <td style="width:<%:widthList[k].Substring(widthList[k].IndexOf("|")+1)%>px;text-align:<%:taList[k].Substring(taList[k].IndexOf("|")+1)%>"><%:val %></td>  
                        <%
                            k++;
                        }
                    %>
                </tr>
                <%}
            if (result1 != null && result1.Rows.Count > 0)
            {
                foreach ( System.Data.DataRow hs in result1.Rows)
                {%>
                   <tr>
                        <td style="width:60px;font-weight:bold;text-align:right">统计：</td>
                       <%
                        int s = 0;
                        foreach(object val in hs.ItemArray)
                        {%>
                            <td style="font-weight:bold;width:<%:widthList[s].Substring(widthList[s].IndexOf("|")+1)%>px;text-align:<%:taList[s].Substring(taList[s].IndexOf("|")+1)%>" title="<%:result1.Columns[s] %>"><%:val %></td>  
                        <%
                            s++;
                        }
                    %>
                   </tr> 
               <%}
            }
        } 
         %>
    </tbody>
</table>
</div>
<div class="panelBar">
    <div class="pages">
        <span>显示</span>
        <select name="numPerPage" onchange="doSearch(1)" class="pageSize">
            <% 
                int[] a = { 10, 20, 50, 100, 200, };
                foreach (var item in a)
                {
                    if (item == PageSize)
                    { 
                        %>
                        <option value="<%= item %>" selected><%= item %></option>
                        <%
                    }
                    else
                    { 
                        %>
                        <option value="<%= item %>"><%= item %></option>
                        <%
                    }
                }
             %>
        </select>
        <span>条，共 <%= totalCount%> 条</span>
    </div>
    <div class="pagination pgt" targetType="navTab" totalCount="<%= totalCount %>" numPerPage="<%= PageSize %>" pageNumShown="5" currentPage="<%= PageNumber %>">
    </div>
</div>
</form>

